// pages/get-money-record/index.js
const handler = require("../../utils/function.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showTimeContainer: false,
    year: [],//时间选择器
    month: [],
    select_data: {},
    new_year: '',
    new_month: '',
    financeData:[],
    spinShow: true,
    currentPage: 1,
    count: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var date = new Date;
    var year = date.getFullYear();
    var month = date.getMonth() + 1;
    month = (month < 10 ? "0" + month : month);
    this.setData({
      new_year: year.toString(),
      new_month: month.toString()
    })
    this.initData();
  },
  initData: function () {

    var para = {
      user_id: wx.getStorageSync('user_info').id,
      limit: this.data.currentPage,
      year: this.data.new_year,
      month: this.data.new_month
    };
    var that = this;
    handler.requestData("withdrawRecord", para, function (res) {
      console.log(res)
      that.setData({ spinShow: false })
      wx.hideNavigationBarLoading(); //完成停止加载
      wx.stopPullDownRefresh(); //停止下拉刷新
      if (res.success == true) {
        that.setData({
          financeData: res.data,
          count: res.data.count[0]['count']
        })
      } else {
        wx.showToast({
          title: res.message,
          icon: 'loading'
        })
      }
    });
  },
  handler_next_page: function (e) {
    handler.intoPageHandler(e.currentTarget.dataset.url);

  },
  appointmentTimeHandler: function (e) {
    this.setData({ showTimeContainer: true })
    this.current_data();
  },
  //时间样式开始
  selectTimeShowTimeHandler: function () {
    this.setData({ showTimeContainer: true, homeHandlerShow: false });
    this.selectTimeHandler(0, 0, 0);
  },
  // getTimeHandlerCancle 预约时间取消样式
  getTimeHandlerCancle: function () {
    this.setData({ showTimeContainer: false, homeHandlerShow: true });
  },
  // getTimeHandlerOk 预约时间确定样式
  getTimeHandlerOk: function () {
    this.setData({ showTimeContainer: false, homeHandlerShow: true });
    this.initData();
  },
  // 绑定时间定时器
  bindTimeChange: function (e) {
    // console.log(e.detail.value)
    if (e.detail.value[0] === 0) {
      this.current_month()
    } else {
      this.other_month()
    }
    const { year, month } = this.data;
    this.setData({ select_data: { year: year[e.detail.value[0]], month: month[e.detail.value[1]] } })
    this.setData({
      new_year: this.data.select_data['year'],
      new_month: this.data.select_data['month']
    })
  },
  current_data: function () {
    var time = new Date();
    var year = time.getFullYear();
    var month = time.getMonth();
    console.log(month)
    var arr_year = [];
    var arr_month = [];
    // 获取年
    for (var i = year; i > year - 10; i--) {
      arr_year[arr_year.length] = i;
    }
    // 获取月
    for (var m = month+1; m > 0; m--) {
      arr_month[arr_month.length] = m;
    }
    this.setData({ year: arr_year, month: arr_month, select_data: { year: arr_year[0], month: [arr_month] } })
  },
  current_month: function () {
    var time = new Date();
    var month = time.getMonth();
    this.data.month.length = 0;
    console.log(month)
    var arr_month = [];
    // 获取月
    for (var m = month+1; m > 0; m--) {
      arr_month[arr_month.length] = m;
    }
    this.setData({ month: arr_month })
  },
  other_month: function () {
    this.data.month.length = 0;
    var arr_month = [];
    // 获取月
    for (var m = 12; m > 0; m--) {
      arr_month[arr_month.length] = m;
    }
    this.setData({ month: arr_month })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.initData()
    wx.showNavigationBarLoading() //在标题栏中显示加载
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var total = this.data.count;
    var current_page = this.data.currentPage;
    if (current_page * 5 > total) {
      console.log("已经是全部数据");
    } else {
      this.setData({
        currentPage: current_page + 1
      })
      this.initData()
      wx.showNavigationBarLoading() //在标题栏中显示加载
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})